package com.example.test05;

import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

// 标记为Spring组件，自动被容器管理
@Component
public class CountdownTask implements Runnable {

    @Override
    public void run() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        int totalSeconds = 60; // 倒计时1分钟

        for (int remaining = totalSeconds; remaining > 0; remaining--) {
            try {
                String currentTime = sdf.format(new Date());
                System.out.printf(
                        "线程[%s] - 当前时间：%s，剩余时间：%d秒%n",
                        Thread.currentThread().getName(),
                        currentTime,
                        remaining
                );
                TimeUnit.SECONDS.sleep(1); // 休眠1秒
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.out.println("任务被中断");
                break;
            }
        }
        System.out.println("线程[" + Thread.currentThread().getName() + "] - 倒计时结束！");
    }
}
